//
//  RequestController.m
//  mindlook
//
//  Created by reedhong on 11-9-27.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "RequestController.h"
#import "Constant.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@implementation RequestController

- (id) init 
{
	self = [super init];
	if (self == nil)
		return nil;
	return self;
}

- (void)dealloc 
{
	[super dealloc];
}

+ (RequestController *)sharedInstance 
{
	static RequestController *instance = nil;
	if (instance == nil) instance = [[RequestController alloc] init];
	return instance;
}

- (NSString*) request:(NSString*) urlStr method:(NSString*) method args:(NSString* )args
{
    NSURL *url = [NSURL URLWithString:urlStr];
    
//	ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
//    [request setRequestMethod:method];
//	[request startSynchronous];
    
    [ASIHTTPRequest clearSession];
	NSArray *sessionCookies = [ASIHTTPRequest sessionCookies];


    
	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
	[request setPostValue:@"xiaohong" forKey:@"username"];
	[request setPostValue:@"beijing" forKey:@"password"];
	[request setShouldStreamPostDataFromDisk:YES];
	[request buildPostBody];
    [request setUseCookiePersistence:YES];
	[request startSynchronous];    
    
    
    NSLog(@"Date:%@", [[request responseHeaders] objectForKey:@"Date"]);
    NSLog(@"Server:%@", [[request responseHeaders] objectForKey:@"Server"]);
    NSLog(@"Vary:%@", [[request responseHeaders] objectForKey:@"Vary"]);
    NSLog(@"Content-Type:%@", [[request responseHeaders] objectForKey:@"Content-Type"]);
    NSLog(@"Location:%@", [[request responseHeaders] objectForKey:@"Location"]);
    NSLog(@"Set-Cookie:%@", [[request responseHeaders] objectForKey:@"Set-Cookie"]);
    //NSLog(@"Date:%@", [[request responseHeaders] objectForKey:@"Date"]);
    

    // cookie
	// Test a cookie is stored in responseCookies
#if 1
	NSArray *cookies = [request responseCookies];
    if(cookies){
        NSLog(@"cookie size %d", [cookies count]);
    }
	NSHTTPCookie *cookie = nil;
	for (cookie in cookies) {
        NSLog(@"%@=%@", [cookie name], [cookie value]);
    }
#endif
    
    
    NSString *html = NULL;
    
    html = [request responseString];
    NSLog(@"status=%d", request.responseStatusCode);
    NSLog(@"html=%@", html);
    NSLog(@"url=%@", [[request url] absoluteString]);
    NSLog(@"originalURL=%@", [[request originalURL] absoluteString]);
    NSLog(@"redirectURL=%@", [[request redirectURL] absoluteString]);
    

    
    if( request.responseStatusCode == 200){
        html = [request responseString];
        
        //NSLog("%@\n", html);
        
    }else if(request.responseStatusCode == 400){
        
    }else if(request.responseStatusCode == 302){

        

    }
    
 
    
    
    return html;
}

- (NSString*) logout
{
    NSString* loginUrlStr = [NSString stringWithFormat:@"%@account/logout/",  REQUEST_SITE];
    NSURL *url = [NSURL URLWithString:loginUrlStr];
    
    // clear session
    [ASIHTTPRequest clearSession];
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    
    if( request.responseStatusCode == 200){
        NSString* html = [request responseString];
        
        NSLog(@"logout result: %@", html);
        return html;
        
    }else if(request.responseStatusCode == 400){
        
    }else if(request.responseStatusCode == 302){
    }
    
    return NULL;    
}

- (NSString *)login:(NSString*)email password:(NSString*)password
{
    NSString* loginUrlStr = [NSString stringWithFormat:@"%@account/login/",  REQUEST_SITE];
    NSURL *url = [NSURL URLWithString:loginUrlStr];
    
    // clear session
    [ASIHTTPRequest clearSession];
    
    email = @"xiaohong";
    password = @"beijing";
    
	ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
	[request setPostValue:email forKey:@"username"];
	[request setPostValue:password forKey:@"password"];
	[request setShouldStreamPostDataFromDisk:YES];
	[request buildPostBody];
    [request setUseCookiePersistence:YES];
	[request startSynchronous];    
    
    NSLog(@"Date:%@", [[request responseHeaders] objectForKey:@"Date"]);
    NSLog(@"Server:%@", [[request responseHeaders] objectForKey:@"Server"]);
    NSLog(@"Vary:%@", [[request responseHeaders] objectForKey:@"Vary"]);
    NSLog(@"Content-Type:%@", [[request responseHeaders] objectForKey:@"Content-Type"]);
    NSLog(@"Location:%@", [[request responseHeaders] objectForKey:@"Location"]);
    NSLog(@"Set-Cookie:%@", [[request responseHeaders] objectForKey:@"Set-Cookie"]);
    //NSLog(@"Date:%@", [[request responseHeaders] objectForKey:@"Date"]);
    
    
    // cookie
	// Test a cookie is stored in responseCookies
#if 1
	NSArray *cookies = [request responseCookies];
    if(cookies){
        NSLog(@"cookie size %d", [cookies count]);
    }
	NSHTTPCookie *cookie = nil;
	for (cookie in cookies) {
        NSLog(@"%@=%@", [cookie name], [cookie value]);
    }
#endif
    
    
    NSString *html = NULL;
    
    html = [request responseString];
    NSLog(@"status=%d", request.responseStatusCode);
    //NSLog(@"html=%@", html);
    NSLog(@"url=%@", [[request url] absoluteString]);
    NSLog(@"originalURL=%@", [[request originalURL] absoluteString]);
    NSLog(@"redirectURL=%@", [[request redirectURL] absoluteString]);
    
    
    
    if( request.responseStatusCode == 200){
        html = [request responseString];
        
        //NSLog("%@\n", html);
        
    }else if(request.responseStatusCode == 400){
        
    }else if(request.responseStatusCode == 302){
        
        
        
    }
    
    return html;
}


- (NSString* )registry:(NSString*)email password:(NSString*)password
{
    return NULL;
}

- (void) testHello
{
    //NSString* testUrlStr = [NSString stringWithFormat:@"%@hello/",  REQUEST_SITE];
    //[self request:testUrlStr method:GET args:NULL];
    
    NSString* testUrlStr = [NSString stringWithFormat:@"%@account/login/",  REQUEST_SITE];
    [self request:testUrlStr method:POST args:NULL];        
}



@end
